var SUCCESS = "success";
var ERROR = "danger";
var INFO = "info";

function showMessage(type, id, msg) {
	var code = "<div class=\"alert alert-"
			+ type
			+ "\">"
			+ "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">\&times;</button>";

	if (type == "success") {
		code += "<b>Success!</b>";
	} else if (type == "danger") {
		code += "<b>Error!</b>";
	}

	code += "&nbsp;&nbsp;<span>" + msg + "</span>" + "</div>";

	$("#" + id).html(code);
	setTimeout(function(){$('#'+id).html('')},10000);
}

function login(){
	var uname = $('#username').val();
	var pass = $('#password').val();
	
	if(uname == "")
	{
		showMessage(ERROR,"login_err_msg", "Please fill the username field.");
		$('#user-name').focus();
		return;
	}
	
	if(pass == "")
	{
		showMessage(ERROR,"login_err_msg", "Please fill the password field.");
		$('#user-password').focus();
		return;		
	}

//	console.debug("",uname);
	
	$.post("login.php",{
				name : uname,
				pass : pass
			},
		function(response){
			if(response.success){
				window.location="login/";			
			}else{
				showMessage(ERROR,"login_err_msg", response.message);
			}
		},
	"json");

}


$(document).ready(function(){

});

function sinup(){
	var school= document.getElementById('signup_school');
	var name= document.getElementById('signup_name');
	var email= document.getElementById('signup_email');
	var phone= document.getElementById('signup_phone');
	var state= document.getElementById('signup_state');
	var city= document.getElementById('signup_city');
	var passwd= document.getElementById('signup_passwd');
	var confpasswd= document.getElementById('signup_confpasswd');
	
	if(school.value == '' || name.value == '' || email.value == '' || phone.value == '' || state.value == '' || city.value == '' || passwd.value == '' || confpasswd.value == '' )
		return;
		
	if(passwd.value != confpasswd.value)
		return;
		
	var data = {
		"school": school.value,
		"name": name.value,
		"email": email.value,
		"phone": phone.value,
		"state": state.value,
		"city": city.value,
		"passwd": passwd.value
	}; 
		
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/signUp.php";
	}else{
		url="php/signUp.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		console.debug(data);
		if(data.error == "false"){
			school.value = '';
			name.value = '';
			email.value = '';
			phone.value = '';
			state.value = '';
			city.value = '';
			passwd.value = '';
			confpasswd.value = '';
		}
		
		$("#alertMsg").html("Thank you for your interest in LifeInClass.com. We will get back to you as soon as possible. Wait for the confirmation call from LifeInClass.com. Have a nice day");
		$('#alertModal').modal('show');
	},
	error: function(e) {
		console.debug(e);
	}
	});
	//window.location.href = "schoolcontrolpanel.html";
}


function contactUs(){
	var contact_name= document.getElementById('contact_name');
	var contact_school= document.getElementById('contact_school');
	var contact_email= document.getElementById('contact_email');
	var contact_contact= document.getElementById('contact_contact');
	var contact_city= document.getElementById('contact_city');
	var contact_state= document.getElementById('contact_state');
	var contact_subject= document.getElementById('contact_subject');
	var contact_message= document.getElementById('contact_message');
	
	if(contact_name.value == '' || contact_message.value == '')
		return;
	
	var data = {
		"name": contact_name.value,
		"school": contact_school.value,
		"email": contact_email.value,
		"contact": contact_contact.value,
		"city": contact_city.value,
		"state": contact_state.value,
		"subject": contact_subject.value,
		"message": contact_message.value
	}; 
		
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/contactUs.php";
	}else{
		url="php/contactUs.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		console.debug(data);
		if(data.error == "false"){
			contact_name.value = '';
			contact_school.value = '';
			contact_email.value = '';
			contact_contact.value = '';
			contact_city.value = '';
			contact_state.value = '';
			contact_subject.value = '';
			contact_message.value = '';
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
	
}

var SUCCESS = "success";
var ERROR = "danger";
var INFO = "info";

function showMessage(type, id, msg) {
	var code = "<div class=\"alert alert-"
			+ type
			+ "\">"
			+ "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">\&times;</button>";

	if (type == "success") {
		code += "<b>Success!</b>";
	} else if (type == "danger") {
		code += "<b>Error!</b>";
	}

	code += "&nbsp;&nbsp;<span>" + msg + "</span>" + "</div>";

	$("#" + id).html(code);
	setTimeout(function(){$('#'+id).html('')},10000);
}

function login(){
	var username = $('#user-name').val();
	var password = $('#user-password').val();
	
	if(username == "")
	{
		showMessage(ERROR,"login_err_msg", "Please fill the username field.");
		$('#user-name').focus();
		return;		
	}
	
	if(password == "")
	{
		showMessage(ERROR,"login_err_msg", "Please fill the password field.");
		$('#user-password').focus();
		return;		
	}

	window.location="login/";
}

var schoolTable;
var cityTable;
var areaTable;
$(document).ready(function(){
	schoolTable= $('#school-tbl').dataTable({
		"aoColumns" : [{
			"bVisible" : false
		},null,null,null,null,null,null,null]
	});
	
	cityTable= $('#city-tbl').dataTable({
		"aoColumns" : [{
			"bVisible" : false
		},null,null,{
			"bSortable" : false
		},null]
	});
	
	areaTable= $('#area-tbl').dataTable({
		"aoColumns" : [{
			"bVisible" : false
		},{
			"bSortable" : false
		},null,{
			"bSortable" : false
		},{
			"bSortable" : false
		},{
			"bSortable" : false
		},{
			"bSortable" : false
		}]
	});
	
	$('#school-tbl tbody tr td img').on('click', function () {
        var nTr = $(this).parents('tr')[0];
        if ( schoolTable.fnIsOpen(nTr) )
        {
            /* This row is already open - close it */
            this.src = "../../bootstraproot/css/DT_bootstrap/images/details_open.png";
            schoolTable.fnClose( nTr );
        }
        else
        {
            /* Open this row */
            this.src = "../../bootstraproot/css/DT_bootstrap/images/details_close.png";
            schoolTable.fnOpen( nTr, fnFormatDetails(schoolTable, nTr), 'details' );
            $('#student-tbl').dataTable({
				"aoColumns" : [{
					"bVisible" : false
				},null,null,null,null,null,null,null,null]
			});
        }
    });
    
/*    getcity();
    getarea();*/
});

function fnFormatDetails ( oTable, nTr )
{
//	var needToSpecifyId = "";
//    var aData = oTable.fnGetData( nTr );
    var sOut = '<div class="tabbable">';
		sOut +='<ul class="nav nav-tabs" style="margin:0">';
		sOut +='<li class="active"><a href="#schoolpanel1"  data-toggle="tab">Student Registration</a></li>';
		sOut +='<li><a href="#schoolpanel2"  data-toggle="tab">Staff Registration</a></li>';
		sOut +='<li><a href="#schoolpanel3"  data-toggle="tab">Create School Gallery</a></li>';			  
		sOut +='</ul>';
		sOut +='<div class="tab-content" style="background-color:#fff;padding:5px 18px">';
		sOut +='<div id="schoolpanel1" class="tab-pane in active">';
		sOut +='<button class="btn btn-success" onclick="studentRegModal();"><i class="icon-plus-sign"></i></button>';
		sOut +=' <button class="btn btn-danger"><i class="icon-minus-sign"></i></button>';
		sOut +='<table class="table table-striped table-bordered" id="student-tbl">';
		sOut +='			<thead>';
		sOut +='				<tr>';
		sOut +='					<th></th>';
		sOut +='					<th><input type="checkbox"></th>';
		sOut +='					<th>Sl No.</th>';
		sOut +='					<th></th>';
		sOut +='					<th>Name</th>';
		sOut +='					<th>Date of Birth</th>';
		sOut +='					<th>Class</th>';
		sOut +='					<th>Roll No</th>';
		sOut +='					<th>Action</th>';									
		sOut +='				</tr>';
		sOut +='			</thead>';
		sOut +='			<tbody>	';
		sOut +='				<tr>';
		sOut +='					<td></td>';
		sOut +='					<th><input type="checkbox"></th>';
		sOut +='					<td>1</td>';
		sOut +='					<td><img src="../../bootstraproot/css/DT_bootstrap/images/details_open.png"></td>';
		sOut +='					<td>Vinay</td>';
		sOut +='					<td>21-11-1990</td>';
		sOut +='					<td>4th</td>';
		sOut +='					<td>44</td>';
		sOut +='					<td><button><i class="icon-pencil"></i></button> <button><i class="icon-trash"></i></button></td>';
		sOut +='				</tr>';
		sOut +='			</tbody>';
		sOut +='		</table>';
		sOut +='</div>';
		sOut +='<div id="schoolpanel2" class="tab-pane">';
		sOut +='<button class="btn btn-success"><i class="icon-plus-sign"></i></button>';
		sOut +=' <button class="btn btn-danger"><i class="icon-minus-sign"></i></button>';
		sOut +='</div>';
		sOut +='<div id="schoolpanel3" class="tab-pane">';
		sOut +='<button class="btn btn-success"><i class="icon-plus-sign"></i></button>';
		sOut +=' <button class="btn btn-danger"><i class="icon-minus-sign"></i></button>';
		sOut +='</div>';
		sOut +='</div>';
    return sOut;
}

function schoolRegModal(){
	$('#school-reg-modal').modal('show');
}

function studentRegModal(){
	$('#student-reg-modal').modal('show');
}

function addcity(){
	var city = $('#cityname').val();
	if(city == ""){
		showMessage(ERROR,"city-err","Please enter the city name.");
		return;
	}
	$.post("addcity.php",{
			city : city
		},
		function(response){
			if(response.success){
				$('#cityname').val('');
				getcity();
			}
		},
	"json");
}

function getcity(){
	$.post("getcity.php",{
		},
		function(response){
			if(response.success){
				cityTable.fnClearTable();
				var cnt = 1;
				var code = '';
				for(i in response.list){					
					var item = response.list[i];					
					cityTable.fnAddData([ 
										item.id,
										'<input type="checkbox">',
							            cnt,
										item.name,
										'<button class="btn btn-small btn-danger" onclick="deletecity('+item.id+');"><i class="icon-minus-sign"></i></button> <button class="btn btn-small btn-warning" onclick="editcity('+item.id+');"><i class="icon-pencil"></i></button>'
					
					]);		
					cnt++;		
					code +='<option value="'+item.id+'">'+item.name+'</option>';
				}
				$('#select-city').html(code);
				$('#school-city').html(code);
			}
		},
	"json");
}


function addArea(){//TODO 
	var cityId = $('#select-city option:selected').val();
	var area = $('#areaname').val();
	var file = $('#areapic1').val();
	if(area == ""){
		showMessage(ERROR,"area-err","Please enter the city name.");
		return;
	}
	$.post("addarea.php",{
			cityId : cityId,
			area : area,
			areapic:file
		},
		function(response){
			if(response.success){
				$('#areaname').val('');
				getarea();
				$('#areapic').submit();
			}
		},
	"json");
}

function getarea(){
	$.post("getarea.php",{
		},
		function(response){
			if(response.success){
				areaTable.fnClearTable();
				var cnt = 1;
				for(i in response.list){					
					var item = response.list[i];					
					if(cnt == 1)
						getCityArea(item.id);
						areaTable.fnAddData([ 
								item.id,
								'<input type="checkbox">',
								cnt,
								item.area,
								item.city,
								'',
								'<button class="btn btn-small btn-danger" onclick="deletecity('+item.id+');"><i class="icon-minus-sign"></i></button> <button class="btn btn-small btn-warning" onclick="editcity('+item.id+');"><i class="icon-pencil"></i></button>'					
					]);		
					cnt++;		
				}
			}
		},
	"json");
}

function getCityArea(id){
	if(id==0)
		id=$('#school-city option:selected').val()
	$.post("getcityarea.php",{
			cityId : id
		},
		function(response){
			if(response.success){
				$('#school-area').prop('disabled',false);
				var code = "";
				for(i in response.list){
					var item = response.list[i];
					code +='<option value="'+item.id+'">'+item.area+'</option>';
				}
				$('#school-area').html(code);
			}else{
				
			}
		},
	"json");
}

function registerSchool(){
	var schoolId = $('#school-id').val();
	var schoolName = $('#school-name').val();	
	var schoolPrincipal = $('#school-principal').val();
	var principalQualification = $('#principal-qualification').val();
	var schoolEmail = $('#school-email').val();
	var schoolPhone = $('#school-phone').val();
	var schoolClassFrom = $('#school-class-from').val();
	var schoolClassTo = $('#school-class-to').val();
	var schoolTests = $('#school-tests').val();
	var schoolCity = $('#school-city option:selected').val();
	var schoolArea = $('#school-area option:selected').val();
	var schoolAddress = $('#school-address').val();
	var schoolGreetings = $('#school-greetings').val();
	
	//TODO Validation of the fields to be done.
	
	$.post("registerSchool.php",{
			schoolId : schoolId,
			schoolName : schoolName,
			schoolPrincipal : schoolPrincipal,
			principalQualification : principalQualification,
			schoolEmail : schoolEmail,
			schoolPhone : schoolPhone,
			schoolClassFrom : schoolClassFrom,
			schoolClassTo : schoolClassTo,
			schoolTests : schoolTests,
			schoolCity : schoolCity,
			schoolArea : schoolArea,
			schoolAddress : schoolAddress,
			schoolGreetings : schoolGreetings
		},
		function(response){
			if(response.success){
				showMessage(SUCCESS, "school-msg-id", response.message);
				getSchools(0);
			}else{
				
			}
		},
	"json");	
}

function getSchools(id){
	$.post("getSchools.php",{
			id : id
		},
		function(response){
			if(response.success){

				
			}
		},
	"json");
}

